package com.iptv.grape.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.FocusFinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.*;

import com.iptv.grape.R;
import com.iptv.grape.ui.control.ad.LabelAdapter;
import com.iptv.grape.util.AppLog;

/**
 *
 * 回看底部水平列表控件
 *
 *
 * **/
public class LongEnterRecycleView extends RecyclerView {
    // 当前的焦点的索引
    private int curIndex = 0;
    // 当前正在播放的索引(被选择的)
    private int curPlayIndex = 0;
    // 总个数
    private int itemCount = 0;

    // 是否拥有焦点
    private boolean hasFocus = false;

    public boolean isHasFocus() {
        return hasFocus;
    }

    public void setHasFocus(boolean hasFocus) {
        this.hasFocus = hasFocus;
    }

    public void setItemCount(int itemCount){
        this.itemCount = itemCount;
    }
    public void setCurIndex(int curIndex){
        this.curIndex = curIndex;
    }
    public void setCurPlayIndex(int curPlayIndex){
        this.curPlayIndex = curIndex;
    }

    public void reSetFocus(){
        LabelAdapter.LabelHolder newHolder = getLabelHolder(curIndex);
        if(newHolder != null){
            newHolder.getLinearLayout().requestFocus();
        }
    }

    // 设置当前的正在播放的LabelHolder的样式
    public void setViewHolderBackgroundByPlay(){
        LabelAdapter.LabelHolder newHolder = getLabelHolder(curPlayIndex);

        if(newHolder != null && this.hasFocus){
            newHolder.getLinearLayout().setBackgroundResource(R.drawable.item_horitation_fucs);
            if(this.hasFocus) {
                //AppLog.getCurLog().out("set jiao dian2");
                ViewCompat.animate(newHolder.getLinearLayout()).scaleX(1.01f).scaleY(1.08f).start();
                ViewGroup parent = (ViewGroup) newHolder.getLinearLayout().getParent();
                parent.requestLayout();
                parent.invalidate();
            }
        }
    }

    // 设置ViewHolder背景颜色
    public void setViewHolderBackground(String billType){
        LabelAdapter.LabelHolder oldHolder = getLabelHolder(this.curPlayIndex);
        if(oldHolder != null) {
            //AppLog.getCurLog().out("set jiao dian1");
            if(billType.equals("before")){
                oldHolder.getLinearLayout().setBackgroundResource(R.drawable.item_red_blus);
            }else if(billType.equals("future")){
                oldHolder.getLinearLayout().setBackgroundResource(R.drawable.item_gray_blus);
            }else {
                oldHolder.getLinearLayout().setBackgroundResource(R.drawable.item_yellow_blus);
            }
        }
        else {
            //AppLog.getCurLog().out("setViewHolderBackground" + "找不到oldHolder");
            this.getAdapter().notifyItemChanged(this.curPlayIndex, 101);
        }

    }

    // 构造函数
    public LongEnterRecycleView(Context context) {
        super(context);
        //setItemViewCacheSize(0);
    }
    // 构造函数
    public LongEnterRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //setItemViewCacheSize(0);
    }


    // 获得指定下标的LabelHolder
    public LabelAdapter.LabelHolder getLabelHolder(int pos){
        return (LabelAdapter.LabelHolder)findViewHolderForAdapterPosition(pos);
    }

    // 当前获得焦点的索引的元素 来请求焦点
    public void getCurLabelHolderRequestFocus(){
        LabelAdapter.LabelHolder vHolder = (LabelAdapter.LabelHolder)findViewHolderForAdapterPosition(this.curIndex);
        vHolder.getRelativeLayout().requestFocus();
    }


    // 初始化元素位置
    public void initScrollToPosition(){
        //this.smoothScrollToPosition(getScrollToPosition(1));
        int pos = curIndex -2;
        if(pos < 0){
            pos = 0;
        }
        this.scrollToPosition(pos);
    }
    // 滚动到下一个位置
    public void scrollToNext(int selectIndex){
        //this.curPlayIndex = selectIndex;

        int dir = 1;
        if(curIndex - curPlayIndex > 0){
            dir = -1;
        }
        this.curIndex = selectIndex;
        //this.smoothScrollToPosition(getScrollToPosition(1));
        this.smoothScrollToPosition(curIndex);
        LabelAdapter.LabelHolder holder = (LabelAdapter.LabelHolder)findViewHolderForAdapterPosition(selectIndex);
        if(holder == null){
            AppLog.getCurLog().out("找不到元素");
        }else {
            AppLog.getCurLog().out("设置元素");
            holder.getLinearLayout().setBackgroundResource(R.drawable.item_gray_blus);
        }

    }

    public LinearLayoutManager  getLinearLayoutManager(){
        return (LinearLayoutManager)getLayoutManager();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int dir = 0;
        //int childCount = this.getChildCount();
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            int keyCode = event.getKeyCode();
            // 这里只考虑水平移动的情况（垂直移动相同的解决方案）
            if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                View focusedView = getFocusedChild();  // 获取当前获得焦点的view

                View nextFocusView;
                try {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                        dir = -1;
                        // 通过findNextFocus获取下一个需要得到焦点的view
                        nextFocusView = FocusFinder.getInstance().findNextFocus(this, focusedView, View.FOCUS_LEFT);
                    } else {
                        dir = 1;
                        // 通过findNextFocus获取下一个需要得到焦点的view
                        nextFocusView = FocusFinder.getInstance().findNextFocus(this, focusedView, View.FOCUS_RIGHT);
                    }
                } catch (Exception e) {
                    nextFocusView = null;
                }
                // 如果获取失败（也就是说需要交给系统来处理焦点， 消耗掉事件，不让系统处理， 并让先前获取焦点的view获取焦点）
                if (nextFocusView == null) {
                    focusedView.requestFocus();
                    return true;
                }
            }
        }
        // 焦点会移动到 下一个元素
        if(dir != 0){
            curIndex = curIndex + dir;
            if(curIndex < 0){
                curIndex = 0;
            }
            if(curIndex >= itemCount){
                curIndex = itemCount - 1;
            }
            this.smoothScrollToPosition(curIndex);
            //this.smoothScrollToPosition(getScrollToPosition(dir));

        }
//        if (event.getAction() == KeyEvent.ACTION_DOWN) {
//            AppLog.getCurLog().out("curIndex=" + curIndex + " childCount=" + childCount);
//            LinearLayoutManager LinearLayoutManager = getLinearLayoutManager();
//            int f = LinearLayoutManager.findFirstVisibleItemPosition();
//            int l = LinearLayoutManager.findLastVisibleItemPosition();
//            AppLog.getCurLog().out("第一个可见" + f + " 最后一个可见" + l);
//        }
        return super.dispatchKeyEvent(event);
    }

//    public int getScrollToPosition(int dir){
//        int pos = curIndex + dir * 2;
//        if(pos >= this.itemCount){
//            pos = this.itemCount;
//        }
//        if(pos < 0){
//            pos = 0;
//        }
//        //AppLog.getCurLog().out("pos=" + pos);
//        return pos;
//
//    }


}
